package com.example.mylibrary;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;

import com.example.mylibrary.config.Config;
import com.example.mylibrary.greendao.DaoMaster;
import com.example.mylibrary.greendao.DaoSession;
import com.example.mylibrary.util.ACacheUtil;
import com.example.mylibrary.util.CrashHandler;

import java.io.File;
import java.util.ArrayList;
import java.util.List;



/**
 * Created by Administrator on 2017/12/19.
 */

public class MyApp extends Application {
    public static final String DB_NAME = "DataInfo";
    private static MyApp mInstance ;
    private static DaoMaster daoMaster;
    private static DaoSession daoSession;
    public static int faceDetectRectShowCount = 0;
    private  SharedPreferences preferences;
    public static  ACacheUtil mACache;
    public static  ACacheUtil mACache1;
    public static  ACacheUtil mAcache2;
    private List<String> filter = new ArrayList<String>();
    @Override
    public void onCreate() {
        super.onCreate();
        mACache = ACacheUtil.get(this);
        mACache1 = ACacheUtil.get(this);
        mAcache2 = ACacheUtil.get(this);
        if (mInstance == null)
            mInstance = this;

        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(mInstance);
        if (!checkFile()) {
            preferences = getSharedPreferences(Config.default_preferences_name,
                    Context.MODE_PRIVATE);
            preferences.edit()
                    .putString(Config.KEY_FINGER_SWITCH, Config.FINGER_OPEN)
                    .commit();
            preferences.edit().putString(Config.KEY_SAVE_PIC, Config.NO_SAVE_PIC)
                    .commit();
            preferences.edit().putInt(Config.KEY_SHUTTER_MODE, Config.MODE_AUTO)
                    .commit();
        }else {
            preferences = getSharedPreferences(Config.default_preferences_name,
                    Context.MODE_PRIVATE);
        }

        GreenDaoManager();

    }
    private boolean checkFile() {
        File file = new File(Config.preference_path);
        return file.exists();
    }

    public static  DaoMaster  getDaoMaster(Context context){
        if (daoMaster == null) {
            DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(context, DB_NAME, null);
            daoMaster = new DaoMaster(helper.getWritableDatabase());
        }
        return daoMaster;
    }


    public void GreenDaoManager() {
        //创建数据库
        DaoMaster.DevOpenHelper helper = new
                DaoMaster.DevOpenHelper(this, "DataInfo.db", null);
        //获得可写入数据库
        SQLiteDatabase database = helper.getWritableDatabase();
        //获取数据库对象
        daoMaster = new DaoMaster(database);
        //获取Dao管理者
        daoSession = daoMaster.newSession();
    }

    public static DaoSession getDaoSession(Context context){
        if (daoSession == null) {
            if (daoMaster == null) {
                daoMaster = getDaoMaster(context);
            }
            daoSession = daoMaster.newSession();
        }
        return daoSession;
    }

    public static DaoSession getDaoInstant( ){

        return daoSession;
    }

    public void setFilter(List<String> filter) {
        this.filter = filter;
    }


    public SharedPreferences getPreferences() {
        return preferences;
    }


    public static MyApp getInstance() {
        return mInstance;
    }

    public Context getContext(){
        return this.getApplicationContext();
    }
}
